﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>Ping: src/main/java/com/stateforge/statemachine/examples/ping/EchoClient.java Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                Examples using state machines generated by StateBuilderJava
            </h1>
            <h3>
                Ping: src/main/java/com/stateforge/statemachine/examples/ping/EchoClient.java Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Packages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">src/main/java/com/stateforge/statemachine/examples/ping/EchoClient.java</div>  </div>
</div>
<div class="contents">
<a href="_echo_client_8java.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a><a class="code" href="namespacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping.html">00002</a> <span class="keyword">package </span>com.stateforge.statemachine.examples.ping;
<a name="l00003"></a>00003 
<a name="l00004"></a>00004 <span class="keyword">import</span> java.net.InetSocketAddress;
<a name="l00005"></a>00005 <span class="keyword">import</span> java.util.concurrent.Executors;
<a name="l00006"></a>00006 <span class="keyword">import</span> java.util.logging.Level;
<a name="l00007"></a>00007 <span class="keyword">import</span> java.util.logging.Logger;
<a name="l00008"></a>00008 
<a name="l00009"></a>00009 <span class="keyword">import</span> org.jboss.netty.bootstrap.ClientBootstrap;
<a name="l00010"></a>00010 <span class="keyword">import</span> org.jboss.netty.buffer.ChannelBuffer;
<a name="l00011"></a>00011 <span class="keyword">import</span> org.jboss.netty.buffer.ChannelBuffers;
<a name="l00012"></a>00012 <span class="keyword">import</span> org.jboss.netty.channel.ChannelHandlerContext;
<a name="l00013"></a>00013 <span class="keyword">import</span> org.jboss.netty.channel.ChannelPipeline;
<a name="l00014"></a>00014 <span class="keyword">import</span> org.jboss.netty.channel.ChannelPipelineFactory;
<a name="l00015"></a>00015 <span class="keyword">import</span> org.jboss.netty.channel.ChannelStateEvent;
<a name="l00016"></a>00016 <span class="keyword">import</span> org.jboss.netty.channel.Channels;
<a name="l00017"></a>00017 <span class="keyword">import</span> org.jboss.netty.channel.ExceptionEvent;
<a name="l00018"></a>00018 <span class="keyword">import</span> org.jboss.netty.channel.MessageEvent;
<a name="l00019"></a>00019 <span class="keyword">import</span> org.jboss.netty.channel.SimpleChannelUpstreamHandler;
<a name="l00020"></a>00020 <span class="keyword">import</span> org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
<a name="l00021"></a>00021 
<a name="l00022"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html">00022</a> <span class="keyword">public</span> <span class="keyword">class </span><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html">EchoClient</a> <span class="keyword">extends</span> SimpleChannelUpstreamHandler {
<a name="l00023"></a>00023 
<a name="l00024"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aebf730f5f302f2bc3adad1b88cb7237c">00024</a>     <span class="keyword">private</span> ClientBootstrap <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aebf730f5f302f2bc3adad1b88cb7237c">bootstrap</a>;
<a name="l00025"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a2592cf243c9b487703cde523c84ae0f0">00025</a>     <span class="keyword">private</span> String <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a2592cf243c9b487703cde523c84ae0f0">target</a> = <span class="stringliteral">&quot;127.0.0.1&quot;</span>;
<a name="l00026"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a8f43c72b16f16b4abc336ca0bc74fd33">00026</a>     <span class="keyword">private</span> <span class="keywordtype">int</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a8f43c72b16f16b4abc336ca0bc74fd33">port</a> = 2011;
<a name="l00027"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac4f51a4de8fad751ad22bdc40cfc34fa">00027</a>     <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac4f51a4de8fad751ad22bdc40cfc34fa">logger</a> = Logger.getLogger(
<a name="l00028"></a>00028             <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html">EchoClient</a>.class.getName());
<a name="l00029"></a>00029 
<a name="l00030"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aa34fe6f1e25a9a08d07cfa4ffd943344">00030</a>     <span class="keyword">private</span> <span class="keyword">final</span> ChannelBuffer <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aa34fe6f1e25a9a08d07cfa4ffd943344">channelBuffer</a>;
<a name="l00031"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ae36dbf32fff9e79b38e045ba35ddc15c">00031</a>     <span class="keyword">private</span> <a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_echo_client_listener.html">IEchoClientListener</a> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ae36dbf32fff9e79b38e045ba35ddc15c">listener</a>;
<a name="l00032"></a>00032     
<a name="l00033"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#abdca79c5b69202830b36127143494651">00033</a>     <span class="keyword">public</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#abdca79c5b69202830b36127143494651">EchoClient</a>(<a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_echo_client_listener.html">IEchoClientListener</a> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ae36dbf32fff9e79b38e045ba35ddc15c">listener</a>){
<a name="l00034"></a>00034         this.listener = <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ae36dbf32fff9e79b38e045ba35ddc15c">listener</a>;
<a name="l00035"></a>00035         <span class="comment">// Configure the client.</span>
<a name="l00036"></a>00036         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aebf730f5f302f2bc3adad1b88cb7237c">bootstrap</a> = <span class="keyword">new</span> ClientBootstrap(
<a name="l00037"></a>00037                 <span class="keyword">new</span> NioClientSocketChannelFactory(
<a name="l00038"></a>00038                         Executors.newCachedThreadPool(),
<a name="l00039"></a>00039                         Executors.newCachedThreadPool()));
<a name="l00040"></a>00040 
<a name="l00041"></a>00041         <span class="comment">// Set up the pipeline factory.</span>
<a name="l00042"></a>00042         <span class="keyword">final</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html">EchoClient</a> me = <span class="keyword">this</span>;
<a name="l00043"></a>00043         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aebf730f5f302f2bc3adad1b88cb7237c">bootstrap</a>.setPipelineFactory(<span class="keyword">new</span> ChannelPipelineFactory() {
<a name="l00044"></a>00044             <span class="keyword">public</span> ChannelPipeline getPipeline() <span class="keywordflow">throws</span> Exception {
<a name="l00045"></a>00045                 <span class="keywordflow">return</span> Channels.pipeline(me);
<a name="l00046"></a>00046             }
<a name="l00047"></a>00047         }); 
<a name="l00048"></a>00048         
<a name="l00049"></a>00049         <span class="keywordtype">int</span> firstMessageSize = 256;
<a name="l00050"></a>00050         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aa34fe6f1e25a9a08d07cfa4ffd943344">channelBuffer</a> = ChannelBuffers.buffer(firstMessageSize);
<a name="l00051"></a>00051         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aa34fe6f1e25a9a08d07cfa4ffd943344">channelBuffer</a>.capacity(); i ++) {
<a name="l00052"></a>00052             <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aa34fe6f1e25a9a08d07cfa4ffd943344">channelBuffer</a>.writeByte((byte) i);
<a name="l00053"></a>00053         }
<a name="l00054"></a>00054     }
<a name="l00055"></a>00055     
<a name="l00056"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac65d3b43a4d13f4ec082fe2164ec4e4c">00056</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac65d3b43a4d13f4ec082fe2164ec4e4c">stop</a>(){
<a name="l00057"></a>00057         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aebf730f5f302f2bc3adad1b88cb7237c">bootstrap</a>.releaseExternalResources();
<a name="l00058"></a>00058     }
<a name="l00059"></a>00059 
<a name="l00060"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ada108fd025e97facfe93d4c91f4e9fa0">00060</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ada108fd025e97facfe93d4c91f4e9fa0">send</a>(){
<a name="l00061"></a>00061         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac4f51a4de8fad751ad22bdc40cfc34fa">logger</a>.log(Level.INFO, <span class="stringliteral">&quot;send&quot;</span>);
<a name="l00062"></a>00062         <span class="comment">// Start the connection attempt.</span>
<a name="l00063"></a>00063         this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aebf730f5f302f2bc3adad1b88cb7237c">bootstrap</a>.connect(<span class="keyword">new</span> InetSocketAddress(<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a2592cf243c9b487703cde523c84ae0f0">target</a>, <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a8f43c72b16f16b4abc336ca0bc74fd33">port</a>)); 
<a name="l00064"></a>00064     }
<a name="l00065"></a>00065 
<a name="l00066"></a>00066     @Override
<a name="l00067"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a0bfb6868def92ed20d85b72f9aa2359c">00067</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a0bfb6868def92ed20d85b72f9aa2359c">channelConnected</a>(
<a name="l00068"></a>00068             ChannelHandlerContext ctx, ChannelStateEvent e) {
<a name="l00069"></a>00069         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac4f51a4de8fad751ad22bdc40cfc34fa">logger</a>.log(Level.INFO, <span class="stringliteral">&quot;channelConnected&quot;</span>);
<a name="l00070"></a>00070         e.getChannel().write(this.<a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#aa34fe6f1e25a9a08d07cfa4ffd943344">channelBuffer</a>);
<a name="l00071"></a>00071     }
<a name="l00072"></a>00072 
<a name="l00073"></a>00073     @Override
<a name="l00074"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a9b7b9a2f76a98e27ae4f4aba74639de7">00074</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a9b7b9a2f76a98e27ae4f4aba74639de7">messageReceived</a>(
<a name="l00075"></a>00075             ChannelHandlerContext ctx, MessageEvent e) {
<a name="l00076"></a>00076         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac4f51a4de8fad751ad22bdc40cfc34fa">logger</a>.log(Level.INFO, <span class="stringliteral">&quot;messageReceived&quot;</span>);
<a name="l00077"></a>00077         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ae36dbf32fff9e79b38e045ba35ddc15c">listener</a>.<a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_echo_client_listener.html#a962bf66e2d32d212548a378cc150af2d">messageReceived</a>();
<a name="l00078"></a>00078     }
<a name="l00079"></a>00079 
<a name="l00080"></a>00080     @Override
<a name="l00081"></a><a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a596f0999e7e5326207fc4a565b71dd70">00081</a>     <span class="keyword">public</span> <span class="keywordtype">void</span> <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#a596f0999e7e5326207fc4a565b71dd70">exceptionCaught</a>(
<a name="l00082"></a>00082             ChannelHandlerContext ctx, ExceptionEvent e) {
<a name="l00083"></a>00083         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ac4f51a4de8fad751ad22bdc40cfc34fa">logger</a>.log(Level.WARNING, <span class="stringliteral">&quot;Unexpected exception from downstream.&quot;</span>, e.getCause());
<a name="l00084"></a>00084         e.getChannel().close();
<a name="l00085"></a>00085         <a class="code" href="classcom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_echo_client.html#ae36dbf32fff9e79b38e045ba35ddc15c">listener</a>.<a class="code" href="interfacecom_1_1stateforge_1_1statemachine_1_1examples_1_1ping_1_1_i_echo_client_listener.html#a8cf27e0a9ea88abee221b58930ca4375">error</a>(e.getCause().getMessage());
<a name="l00086"></a>00086     }
<a name="l00087"></a>00087 }
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
